package com.iotproject.eduservice.service.impl;

import com.iotproject.eduservice.entity.EduChapter;
import com.iotproject.eduservice.entity.EduSection;
import com.iotproject.eduservice.entity.chapter.ChapterVo;
import com.iotproject.eduservice.entity.chapter.VideoVo;
import com.iotproject.eduservice.mapper.EduChapterMapper;
import com.iotproject.eduservice.service.EduChapterService;
import com.iotproject.eduservice.service.EduVideoService;
import com.iotproject.servicebase.exceptionhandler.GlobalException;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2022-11-08
 */
@Service
public class EduChapterServiceImpl extends ServiceImpl<EduChapterMapper, EduChapter> implements EduChapterService {
    @Autowired
    EduVideoService eduVideoService;
    //根据课程id得到课程大纲列表
    @Override
    public List<ChapterVo> getChapterVideoByCourseId(String courseId) {
        //得到章节
        QueryWrapper<EduChapter> eduChapterQueryWrapper = new QueryWrapper<>();
        eduChapterQueryWrapper.eq("course_id", courseId);
        List<EduChapter> eduChapters = baseMapper.selectList(eduChapterQueryWrapper);
        //得到小节
        QueryWrapper<EduSection> eduVideoQueryWrapper = new QueryWrapper<>();
        eduVideoQueryWrapper.eq("course_id", courseId);
        List<EduSection> eduSections = eduVideoService.list(eduVideoQueryWrapper);
        //封装章节
        List<ChapterVo> res = new ArrayList<>();
        for (int i = 0; i < eduChapters.size(); i++) {
            ChapterVo chapterVo = new ChapterVo();
            BeanUtils.copyProperties(eduChapters.get(i), chapterVo);
            //封装小节
            List<VideoVo> temp = new ArrayList<>();
            for (int m = 0; m < eduSections.size(); m++) {
                VideoVo videoVo = new VideoVo();
                BeanUtils.copyProperties(eduSections.get(m), videoVo);
                if (eduSections.get(m).getChapterId().equals(chapterVo.getId())) {//chapter_id相同
                    temp.add(videoVo);
                }
            }
            chapterVo.setChildren(temp);
            res.add(chapterVo);
        }
        return res;
    }

    ////删除章节的方法
    @Override
    public boolean deleteChapter(String chapterId) {
        QueryWrapper<EduSection> eduVideoQueryWrapper = new QueryWrapper<>();
        eduVideoQueryWrapper.eq("chapter_id", chapterId);
        int count = eduVideoService.count(eduVideoQueryWrapper);
        if (count > 0) {//存在小节，不能删除
            throw new GlobalException(20001, "当前章节有小节，不能删除");
        } else {
            int res = baseMapper.deleteById(chapterId);
            return res > 0;
        }
    }

    @Override
    public void deleteByCourseId(String courseId) {
        QueryWrapper<EduChapter> eduChapterQueryWrapper = new QueryWrapper<>();
        eduChapterQueryWrapper.eq("course_id", courseId);
        //删除
        baseMapper.delete(eduChapterQueryWrapper);
    }
}
